<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ai;
use app\common\library\Crawl;
/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = [''];

    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function test($url = null)
    {
        
        $res = Crawl::instance()->crawlEmails($url);
        $this->success('返回成功', $res);
    }

    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $this->success('返回成功', ['action' => 'test1']);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }
   
    public function s(){
        $apiUrl = "https://api.textin.com/ai/service/v1/pdf_to_markdown";
        $headers = [
            "Authorization:Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjI2NzY3OTY4LTNmNzgtNGEwNC04N2EwLTNmMWJiZGRjMGE2MyJ9.Prtp5xdD0TyjZjSF-6q_I8khYL-XblKTLKbvCVKK35dpol6uqHKdhGozvH5O1BN2hYVqZyC_85KViORS7YdhBQ",
            "x-ti-app-id:81d2cbce87b67e4ee2093be2de4af889",
            "x-ti-secret-code:97b29a23e0b36f4799565d8988e6da46",
            "Content-Type: multipart/form-data"

        ];
        $image = "https://aiedm.wchingtech.com/uploads/1.jpg";
        $data = [
            'image' => file_get_contents($image)
        ];
        // 初始化 cURL 會話
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($image));
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        // 執行 cURL 請求
        $response = curl_exec($ch);
        $result = empty($response) ? [] : json_decode($response,true);
        echo $response;
    }

    public function te(){
        $headers = array(
            'Content-Type' => 'application/json',
            'X-API-Key' => 'guHxTqBKTLEZxH3e3sy9JeVuJT9eMA',
        );

        $client = new \GuzzleHttp\Client();

        // Define array of request body.
        $request_body = array();

        try {
            $response = $client->request('GET','https://api-perf.inline.app/reservations/updated/-OYisDoQ1_fNeSqoU3gt:inline-staging-2a466/-OYisDvCdEVdNkUgHQWy', array(
                'headers' => $headers,
                'json' => $request_body,
            )
            );
            print_r($response->getBody()->getContents());
        }
        catch (\GuzzleHttp\Exception\BadResponseException $e) {
            // handle exception or api errors.
            print_r($e->getMessage());
        }
    }
}
